import { message } from "antd";
import { json, save, dele } from "../services/job";

export default {
  namespace: "job",

  // 状态机
  state: {
    data: [], // 表格数据
    total: 0, // 表格记录条数
    success: true, // 操作是否成功(保存和删除是否成功)
  },

  // 发起网络请求
  effects: {
    *json({ payload }, { call, put }) {
      const response = yield call(json, payload);
      yield put({ type: "callbackJson", response });
    },
    *save({ payload }, { call, put }) {
      const response = yield call(save, payload);
      yield put({ type: "callbackSave", response });
    },
    *dele({ payload }, { call, put }) {
      const response = yield call(dele, payload);
      yield put({ type: "callbackDele", response });
    },
  },

  // 回调处理
  reducers: {
    callbackJson(st, { response }) {
      const state = st;
      if (response.success) {
        state.data = response.root.sort((a, b) => b.sequ - a.sequ);
        state.total = response.total;
      } else if (response.status === 1) message.error(response.msg);
      return { ...state };
    },
    callbackDele(st, { response }) {
      const state = st;
      state.success = response.success;
      if (response.success) {
        message.success(response.msg);
      } else if (response.status === 1) message.error(response.msg);
      return { ...state };
    },
    callbackSave(st, { response }) {
      const state = st;
      state.success = response.success;
      if (response.success) {
        message.success(response.msg);
      } else if (response.status === 1) message.error(response.msg);
      return { ...state };
    },
  },
};
